package com.android.shop.ui;

import java.util.ArrayList;
import java.util.List;

import com.android.shop.R;
import com.android.shop.adapter.DishListActivityAdapter;
import com.android.shop.api.MyHttpClient;
import com.android.shop.bean.Dish;
import com.android.shop.constant.MyConstantID;
import com.pulltorefresh.library.PullToRefreshBase.Mode;
import com.pulltorefresh.library.PullToRefreshListView;
import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class DishListTabActivity extends BaseActivity {
	private ViewPager mPager;// 页卡内容
	private List<View> listViews; // Tab页面列表
	private ImageView cursor;// 动画图片
	private TextView t1, t2;// 页卡头标
	private int offset = 0;// 动画图片偏移量
	private int currIndex = 0;// 当前页卡编号
	private int bmpW;// 动画图片宽度

	private PullToRefreshListView listView_xiajia;
	private PullToRefreshListView listView_shangjia;
	private List<Dish> listDish_xiajia;
	private List<Dish> listDish_shangjia;
	private DishListActivityAdapter dishAdapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dish_tab);
		InitImageView();
		InitTextView();
		InitViewPager();
	}

	/**
	 * 初始化头标
	 */
	private void InitTextView() {
		t1 = (TextView) findViewById(R.id.text1);
		t2 = (TextView) findViewById(R.id.text2);

		t1.setOnClickListener(new MyOnClickListener(0));
		t2.setOnClickListener(new MyOnClickListener(1));
	}

	/**
	 * 头标点击监听
	 */
	public class MyOnClickListener implements View.OnClickListener {
		private int index = 0;

		public MyOnClickListener(int i) {
			index = i;
		}

		@Override
		public void onClick(View v) {
			mPager.setCurrentItem(index);
		}
	};

	/**
	 * 初始化ViewPager
	 */
	private void InitViewPager() {
		mPager = (ViewPager) findViewById(R.id.vPager);
		listViews = new ArrayList<View>();
		LayoutInflater mInflater = getLayoutInflater();
		listViews.add(mInflater.inflate(R.layout.dish_list_shangjia, null));
		listViews.add(mInflater.inflate(R.layout.dish_list_xiajia, null));
		mPager.setAdapter(new MyPagerAdapter(listViews));
		mPager.setCurrentItem(0);
		mPager.setOnPageChangeListener(new MyOnPageChangeListener());
		
		new Thread(new ReceiveThread()).start(); // 开启线程获取数据
	}

	/**
	 * ViewPager适配器
	 */
	public class MyPagerAdapter extends PagerAdapter {
		public List<View> mListViews;

		public MyPagerAdapter(List<View> mListViews) {
			this.mListViews = mListViews;
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView(mListViews.get(arg1));
		}

		@Override
		public void finishUpdate(View arg0) {
		}

		@Override
		public int getCount() {
			return mListViews.size();
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			((ViewPager) arg0).addView(mListViews.get(arg1), 0);
			return mListViews.get(arg1);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == (arg1);
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
		}
	}

	/**
	 * 初始化动画
	 */
	private void InitImageView() {
		cursor = (ImageView) findViewById(R.id.cursor);
		bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.tab_b)
				.getWidth();// 获取图片宽度
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;// 获取分辨率宽度
		offset = (screenW / 2 - bmpW) / 2;// 计算偏移量
		Matrix matrix = new Matrix();
		matrix.postTranslate(offset, 0);
		cursor.setImageMatrix(matrix);// 设置动画初始位置
	}

	/**
	 * 页卡切换监听
	 */
	public class MyOnPageChangeListener implements OnPageChangeListener {

		int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量

		@Override
		public void onPageSelected(int arg0) {
			Animation animation = null;
			switch (arg0) {
			case 0:
				if (currIndex == 1) {
					animation = new TranslateAnimation(one, 0, 0, 0);
				}
				break;
			case 1:
				if (currIndex == 0) {
					animation = new TranslateAnimation(offset, one, 0, 0);
				}
				break;
			}
			currIndex = arg0;
			animation.setFillAfter(true);// True:图片停在动画结束位置
			animation.setDuration(300);
			cursor.startAnimation(animation);
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
	}
	
	/*--------------以下是获取数据--------------*/
	class ReceiveThread implements Runnable {
		@Override
		public void run() {
			MyHttpClient myHttpClient = MyHttpClient.getSingle();
			SharedPreferences sp = getSharedPreferences(MyConstantID.SP_NAME,Activity.MODE_PRIVATE);  
			String sellerid= sp.getString("sellerid", "");//获取商户id
			listDish_xiajia = myHttpClient.getDishBySellerid(sellerid,"xiajia");
			listDish_shangjia = myHttpClient.getDishBySellerid(sellerid,"shangjia");
			handler.sendEmptyMessage(11);
		}
	}

	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == 11) {
				if (listDish_xiajia == null||listDish_shangjia==null) {
					Toast.makeText(DishListTabActivity.this, "暂无数据,请去添加菜品",Toast.LENGTH_LONG).show();	
				} else {
					Bind();
				}
			}
		}
	};

	public void Bind() {
		listView_xiajia = (PullToRefreshListView) findViewById(R.id.dish_list_xiajia);
		listView_xiajia.setEmptyView(findViewById(R.id.nodata_xiajia));
		listView_xiajia.setMode(Mode.BOTH);
		dishAdapter = new DishListActivityAdapter(this, listDish_xiajia);
		listView_xiajia.setAdapter(dishAdapter);
		listView_xiajia.setOnItemClickListener(onItemClickListener);
		
		listView_shangjia = (PullToRefreshListView) findViewById(R.id.dish_list_shangjia);
		listView_xiajia.setEmptyView(findViewById(R.id.nodata_shangjia));
		listView_shangjia.setMode(Mode.BOTH);
		dishAdapter = new DishListActivityAdapter(this, listDish_shangjia);
		listView_shangjia.setAdapter(dishAdapter);
		listView_shangjia.setOnItemClickListener(onItemClickListener);
	}

	// listView的点击事件
	OnItemClickListener onItemClickListener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
			
		}
	};
	
	public void back(View v){
		this.finish();
	}
}
